package com.hongxun.design.jungong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.jungong.fragment.FragmentPhoto;
import com.hongxun.design.jungong.fragment.FragmentPhotoListView;
import com.hongxun.design.jungong.fragment.FragmentPhotoWaterMark;
import com.hongxun.design.jungong.model.SspPhoto;
import com.hongxun.design.jungong.model.SspRecord;
import com.hongxun.design.mytools.MString;

/**
 * Created by cqcqwucp on 2017/11/16.随手拍
 */

public class ProRecAndPicActivity extends BaseActivity {
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private FragmentPhoto fragmentPhoto;
    private FragmentPhotoListView photoListView;
    private FragmentPhotoWaterMark photoWaterMark;
    private BaseFragment baseFragment;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prapactivity);
        manager = getSupportFragmentManager();
        fragmentPhoto = new FragmentPhoto();
        photoListView = new FragmentPhotoListView();
        photoWaterMark = new FragmentPhotoWaterMark();
        onBack(0);
    }

    public void onBack(int index, Object... objects) {
        this.index = index;
        baseFragment = null;
        Bundle bundle;
        transaction = manager.beginTransaction();
        switch (index) {
            case 0:
                baseFragment = fragmentPhoto;
                break;
            case 1:
                bundle = new Bundle();
                bundle.putSerializable(MString.key, (SspRecord) objects[0]);
                photoListView.setArguments(bundle);
                baseFragment = photoListView;
                break;
            case 2:
                bundle = new Bundle();
                bundle.putSerializable(MString.key, (SspRecord) objects[0]);
                bundle.putSerializable(MString.boyCode, (SspPhoto) objects[1]);
                bundle.putString(MString.before_number, String.valueOf(objects[2]));
                photoWaterMark.setArguments(bundle);
                baseFragment = photoWaterMark;
                break;
        }
        transaction.replace(R.id.fg_prapa, baseFragment, index + "");
        transaction.commit();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        baseFragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (index == 1) {
            photoListView.onKeyDown(keyCode, event);
            return false;
        } else if (index == 2) {
            photoWaterMark.onKeyDown(keyCode, event);
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
